﻿Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Runtime.CompilerServices
Imports AdvanceWars.Data

Public Module ImageHelper

    ''' <summary>
    ''' 按指定大小切割图像
    ''' </summary>
    ''' <param name="Image">要切割的Bitmap</param>
    ''' <param name="width">每个图像的宽度</param>
    ''' <param name="height">每个图像的高度</param>
    ''' <returns>返回切割后的图像枚举器</returns>
    <Extension()>
    Public Function Splite(ByVal Image As Bitmap, ByVal width As Integer, ByVal height As Integer) As IEnumerable(Of Bitmap)
        Dim list As New List(Of Bitmap)
        Dim c As Integer = Image.Width \ width
        Dim r As Integer = Image.Height \ height
        For i = 0 To r - 1
            For j = 0 To c - 1
                Dim rtg As New Rectangle(j * width, i * height, width, height)
                Dim b As Bitmap = Image.Clone(rtg, PixelFormat.Format24bppRgb)
                list.Add(b)
            Next
        Next
        Return list
    End Function

    ''' <summary>
    ''' 随机画画
    ''' </summary>
    <Extension()>
    Public Sub DrawRandom(ByVal g As Graphics, ByVal images As IEnumerable(Of Bitmap))
        For i = 0 To 29
            For j = 0 To 19
                Dim Img As Bitmap
                Img = images(DataService.Random.Next(0, images.Count - 1))
                Img.MakeTransparent()
                g.DrawImage(Img, i * Img.Width, j * Img.Height, Img.Width, Img.Height)
            Next
        Next
    End Sub

    ''' <summary>
    ''' 合并图像
    ''' </summary>
    <Extension()>
    Public Function GetTableImg(ByVal images As IEnumerable(Of Bitmap), ByVal column As Integer, ByVal size As Size) As Bitmap
        Dim row As Integer = Math.Ceiling(images.Count / column)
        Dim b As New Bitmap(size.Width * column, size.Height * row)
        Dim g As Graphics = Graphics.FromImage(b)
        g.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
        Dim i, w, h As Integer
        h = -size.Height
        For Each img As Bitmap In images
            If i Mod column Then
                w += size.Width
            Else
                h += size.Height
                w = 0
            End If
            img.MakeTransparent()
            g.DrawImage(img, w, h, size.Width, size.Height)
            i += 1
        Next
        Return b
    End Function








#Region "ImageGet"

    <Extension()>
    Public Function GetImage(ByVal Terrain As Terrain, Optional ByVal Type As MapType = 0) As Bitmap
        Return GetTerrainImage(Terrain.Type.Id, Type)
    End Function

    Private Function GetTerrainImage(ByVal id As Integer, Optional ByVal Type As MapType = 0) As Bitmap
        Return ImageService.Terrains(id)
    End Function








    <Extension()>
    Public Function GetImage(ByVal Unit As Unit) As Bitmap
        Return GetUnitImage(Unit.Type.Id, Unit.Color)
    End Function


    Public Function GetImage(ByVal unitId As Integer, ByVal color As Byte) As Bitmap
        Return GetUnitImage(unitId, color)
    End Function



    Private Function GetUnitImage(ByVal id As Integer, ByVal Color As Byte) As Bitmap
        Dim i As Integer = Color * 26 + id
        Return ImageService.Units(i)
    End Function

#End Region

    <Extension()>
    Public Function DrawGame(ByVal game As Game) As Bitmap
        Dim bitmap As New Bitmap(game.Map.Width * 16, game.Map.Height * 16)
        Dim g As Graphics = Graphics.FromImage(bitmap)
        g.SmoothingMode = Drawing2D.SmoothingMode.HighQuality

        For Each t In game.Map.Terrains
            Dim img As Bitmap = t.GetImage
            img.MakeTransparent()
            g.DrawImage(img, t.Location.X * 16, t.Location.Y * 16, img.Width, img.Height)
        Next

        For Each u In game.Map.Units
            Dim img As Bitmap = u.GetImage
            img.MakeTransparent()
            g.DrawImage(img, u.Location.X * 16, u.Location.Y * 16, img.Width, img.Height)
        Next
        Return bitmap
    End Function

End Module